Skills Network Logo

Extracting and Visualizing Stock Data

Description

Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some stock data, you will then display this data in a graph.

Table of Contents

  • Define a Function that Makes a Graph
  • Question 1: Use yfinance to Extract Stock Data
  • Question 2: Use Webscraping to Extract Tesla Revenue Data
  • Question 3: Use yfinance to Extract Stock Data
  • Question 4: Use Webscraping to Extract GME Revenue Data
  • Question 5: Plot Tesla Stock Graph
  • Question 6: Plot GameStop Stock Graph

Estimated Time Needed: 30 min


Note:- If you are working Locally using anaconda, please uncomment the following code and execute it.

In [1]:
!pip install yfinance==0.2.38
!pip install pandas==2.2.2
!pip install nbformat
Defaulting to user installation because normal site-packages is not writeable
Collecting yfinance==0.2.38
  Downloading yfinance-0.2.38-py2.py3-none-any.whl (72 kB)
     ---------------------------------------- 73.0/73.0 kB 4.2 MB/s eta 0:00:00
Requirement already satisfied: numpy>=1.16.5 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from yfinance==0.2.38) (1.21.6)
Requirement already satisfied: appdirs>=1.4.4 in c:\programdata\anaconda3\lib\site-packages (from yfinance==0.2.38) (1.4.4)
Requirement already satisfied: pytz>=2022.5 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from yfinance==0.2.38) (2022.7.1)
Collecting peewee>=3.16.2
  Downloading peewee-3.17.5.tar.gz (3.0 MB)
     ---------------------------------------- 3.0/3.0 MB 3.8 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Requirement already satisfied: html5lib>=1.1 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from yfinance==0.2.38) (1.1)
Requirement already satisfied: beautifulsoup4>=4.11.1 in c:\programdata\anaconda3\lib\site-packages (from yfinance==0.2.38) (4.11.1)
Collecting requests>=2.31
  Downloading requests-2.32.3-py3-none-any.whl (64 kB)
     ---------------------------------------- 64.9/64.9 kB 3.4 MB/s eta 0:00:00
Requirement already satisfied: multitasking>=0.0.7 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from yfinance==0.2.38) (0.0.11)
Requirement already satisfied: frozendict>=2.3.4 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from yfinance==0.2.38) (2.3.4)
Requirement already satisfied: pandas>=1.3.0 in c:\programdata\anaconda3\lib\site-packages (from yfinance==0.2.38) (1.4.4)
Requirement already satisfied: lxml>=4.9.1 in c:\programdata\anaconda3\lib\site-packages (from yfinance==0.2.38) (4.9.1)
Requirement already satisfied: soupsieve>1.2 in c:\programdata\anaconda3\lib\site-packages (from beautifulsoup4>=4.11.1->yfinance==0.2.38) (2.3.1)
Requirement already satisfied: webencodings in c:\programdata\anaconda3\lib\site-packages (from html5lib>=1.1->yfinance==0.2.38) (0.5.1)
Requirement already satisfied: six>=1.9 in c:\programdata\anaconda3\lib\site-packages (from html5lib>=1.1->yfinance==0.2.38) (1.16.0)
Requirement already satisfied: python-dateutil>=2.8.1 in c:\programdata\anaconda3\lib\site-packages (from pandas>=1.3.0->yfinance==0.2.38) (2.8.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests>=2.31->yfinance==0.2.38) (1.26.11)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests>=2.31->yfinance==0.2.38) (2022.9.14)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\programdata\anaconda3\lib\site-packages (from requests>=2.31->yfinance==0.2.38) (2.0.4)
Requirement already satisfied: idna<4,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests>=2.31->yfinance==0.2.38) (3.3)
Building wheels for collected packages: peewee
  Building wheel for peewee (pyproject.toml): started
  Building wheel for peewee (pyproject.toml): finished with status 'done'
  Created wheel for peewee: filename=peewee-3.17.5-py3-none-any.whl size=138681 sha256=ed98946cefdc57cadba51af8e363f7abd360c9664bc54647fba1f436daf24fb5
  Stored in directory: c:\users\alexa\appdata\local\pip\cache\wheels\95\bd\4e\15d1893c245357e4d2353e77456c0be9561d1942ed9fe664ca
Successfully built peewee
Installing collected packages: peewee, requests, yfinance
  Attempting uninstall: yfinance
    Found existing installation: yfinance 0.2.4
    Uninstalling yfinance-0.2.4:
      Successfully uninstalled yfinance-0.2.4
Successfully installed peewee-3.17.5 requests-2.32.3 yfinance-0.2.38
  WARNING: The script sample.exe is installed in 'C:\Users\alexa\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
anaconda-project 0.11.1 requires ruamel-yaml, which is not installed.
conda-repo-cli 1.0.20 requires clyent==1.2.1, but you have clyent 1.2.2 which is incompatible.
conda-repo-cli 1.0.20 requires nbformat==5.4.0, but you have nbformat 5.5.0 which is incompatible.
conda-repo-cli 1.0.20 requires requests==2.28.1, but you have requests 2.32.3 which is incompatible.
Defaulting to user installation because normal site-packages is not writeable
Collecting pandas==2.2.2
  Downloading pandas-2.2.2-cp39-cp39-win_amd64.whl (11.6 MB)
     ---------------------------------------- 11.6/11.6 MB 4.2 MB/s eta 0:00:00
Collecting numpy>=1.22.4
  Downloading numpy-1.26.4-cp39-cp39-win_amd64.whl (15.8 MB)
     ---------------------------------------- 15.8/15.8 MB 3.3 MB/s eta 0:00:00
Requirement already satisfied: pytz>=2020.1 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from pandas==2.2.2) (2022.7.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from pandas==2.2.2) (2023.3)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\programdata\anaconda3\lib\site-packages (from pandas==2.2.2) (2.8.2)
Requirement already satisfied: six>=1.5 in c:\programdata\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas==2.2.2) (1.16.0)
Installing collected packages: numpy, pandas
  Attempting uninstall: numpy
    Found existing installation: numpy 1.21.6
    Uninstalling numpy-1.21.6:
      Successfully uninstalled numpy-1.21.6
  WARNING: The script f2py.exe is installed in 'C:\Users\alexa\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
ERROR: Could not install packages due to an OSError: [WinError 5] Acesso negado: 'C:\\Users\\alexa\\AppData\\Roaming\\Python\\Python39\\site-packages\\~~mpy\\.libs\\libopenblas.XWYDX2IKJW2NMTWSFYNGFUWKQU3LYTCZ.gfortran-win_amd64.dll'
Check the permissions.

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: nbformat in c:\programdata\anaconda3\lib\site-packages (5.5.0)
Requirement already satisfied: traitlets>=5.1 in c:\programdata\anaconda3\lib\site-packages (from nbformat) (5.1.1)
Requirement already satisfied: fastjsonschema in c:\programdata\anaconda3\lib\site-packages (from nbformat) (2.16.2)
Requirement already satisfied: jupyter_core in c:\programdata\anaconda3\lib\site-packages (from nbformat) (4.11.1)
Requirement already satisfied: jsonschema>=2.6 in c:\programdata\anaconda3\lib\site-packages (from nbformat) (4.16.0)
Requirement already satisfied: attrs>=17.4.0 in c:\users\alexa\appdata\roaming\python\python39\site-packages (from jsonschema>=2.6->nbformat) (23.2.0)
Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in c:\programdata\anaconda3\lib\site-packages (from jsonschema>=2.6->nbformat) (0.18.0)
Requirement already satisfied: pywin32>=1.0 in c:\programdata\anaconda3\lib\site-packages (from jupyter_core->nbformat) (302)
In [ ]:
#!pip install yfinance==0.1.67
#!mamba install bs4==4.10.0 -y
#!pip install nbformat==4.2.0
In [5]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots

In Python, you can ignore warnings using the warnings module. You can use the filterwarnings function to filter or ignore specific warning messages or categories.

In [6]:
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)

Define Graphing Function¶

In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.

In [7]:
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()

Question 1: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.

In [8]:
tesla = yf.Ticker("TSLA")

Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.

In [9]:
tesla_data = tesla.history(period="max")

Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.

In [11]:
tesla_data.reset_index(inplace=True)
tesla_data.head()
Out[11]:
Date Open High Low Close Volume Dividends Stock Splits
0 2010-06-29 00:00:00-04:00 1.266667 1.666667 1.169333 1.592667 281494500 0.0 0.0
1 2010-06-30 00:00:00-04:00 1.719333 2.028000 1.553333 1.588667 257806500 0.0 0.0
2 2010-07-01 00:00:00-04:00 1.666667 1.728000 1.351333 1.464000 123282000 0.0 0.0
3 2010-07-02 00:00:00-04:00 1.533333 1.540000 1.247333 1.280000 77097000 0.0 0.0
4 2010-07-06 00:00:00-04:00 1.333333 1.333333 1.055333 1.074000 103003500 0.0 0.0

Question 2: Use Webscraping to Extract Tesla Revenue Data¶

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm Save the text of the response as a variable named html_data.

In [12]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
html_data = requests.get(url).text

Parse the html data using beautiful_soup.

In [13]:
soup = soup = BeautifulSoup(html_data, "html.parser")

Using BeautifulSoup or the read_html function extract the table with Tesla Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.

Click here if you need help locating the table ``` Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab soup.find_all("tbody")[1] If you want to use the read_html function the table is located at index 1 We are focusing on quarterly revenue in the lab. ```
In [14]:
tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])

for row in soup.find_all("tbody")[1].find_all("tr"):
  col = row.find_all("td")
  date = col[0].text
  revenue = col[1].text.replace("$", "").replace(",", "")
  
  tesla_revenue = pd.concat([tesla_revenue, pd.DataFrame([{"Date": date, "Revenue": revenue}])], ignore_index=True)

Execute the following line to remove the comma and dollar sign from the Revenue column.

In [15]:
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")

Execute the following lines to remove an null or empty strings in the Revenue column.

In [16]:
tesla_revenue.dropna(inplace=True)

tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]

Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.

In [17]:
tesla_revenue.tail()
Out[17]:
Date Revenue
48 2010-09-30 31
49 2010-06-30 28
50 2010-03-31 21
52 2009-09-30 46
53 2009-06-30 27

Question 3: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.

In [18]:
gme = yf.Ticker("GME")

Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.

In [19]:
gme_data = gme.history(period="max")

Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.

In [20]:
gme_data.reset_index(inplace=True)
gme_data.head()
Out[20]:
Date Open High Low Close Volume Dividends Stock Splits
0 2002-02-13 00:00:00-05:00 1.620129 1.693350 1.603296 1.691667 76216000 0.0 0.0
1 2002-02-14 00:00:00-05:00 1.712707 1.716074 1.670626 1.683250 11021600 0.0 0.0
2 2002-02-15 00:00:00-05:00 1.683251 1.687459 1.658002 1.674834 8389600 0.0 0.0
3 2002-02-19 00:00:00-05:00 1.666418 1.666418 1.578047 1.607504 7410400 0.0 0.0
4 2002-02-20 00:00:00-05:00 1.615920 1.662210 1.603296 1.662210 6892800 0.0 0.0

Question 4: Use Webscraping to Extract GME Revenue Data¶

Use the requests library to download the webpage https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html. Save the text of the response as a variable named html_data.

In [21]:
url_gme = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html"
html_data_gme = requests.get(url_gme).text

Parse the html data using beautiful_soup.

In [22]:
soup_gme = BeautifulSoup(html_data_gme, 'html.parser')

Using BeautifulSoup or the read_html function extract the table with GameStop Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.

Click here if you need help locating the table ``` Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab soup.find_all("tbody")[1] If you want to use the read_html function the table is located at index 1 ```
In [23]:
gme_revenue = pd.DataFrame(columns=["Date", "Revenue"])

for row in soup_gme.find_all("tbody")[1].find_all("tr"):
  col = row.find_all("td")
  date = col[0].text
  revenue = col[1].text.replace("$", "").replace(",", "")

  gme_revenue = pd.concat([gme_revenue, pd.DataFrame([{"Date": date, "Revenue": revenue}])], ignore_index=True)

Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.

In [24]:
gme_revenue.tail()
Out[24]:
Date Revenue
57 2006-01-31 1667
58 2005-10-31 534
59 2005-07-31 416
60 2005-04-30 475
61 2005-01-31 709

Question 5: Plot Tesla Stock Graph¶

Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla'). Note the graph will only show data upto June 2021.

In [25]:
make_graph(tesla_data, tesla_revenue, 'Tesla')

Question 6: Plot GameStop Stock Graph¶

Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.

In [26]:
make_graph(gme_data, gme_revenue, 'GameStop')

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Azim Hirjani

Change Log¶

Date (YYYY-MM-DD) Version Changed By Change Description
2022-02-28 1.2 Lakshmi Holla Changed the URL of GameStop
2020-11-10 1.1 Malika Singla Deleted the Optional part
2020-08-27 1.0 Malika Singla Added lab to GitLab

© IBM Corporation 2020. All rights reserved.